package c2.advanced.business.demo.handler;

import c2.advanced.business.base.AdvancedSubModule;
import cate.game.play.part.PlaySide;
import cp.solution.rpc.message.RpcMessage;
import cp.solution.rpc.path.RpcDebugPath;
import cp.solution.rpc.path.RpcTestPath;
import cp.solution.rpc.test.TheCat;
import cp.solution.rpc.test.TheDog;
import cp.solution.util.demo.vo.DemoAppleVo;
import cp.solution.util.demo.vo.DemoBananaVo;
import easy.java.struct.ref.IntRef;
import fax.common.util.ThreadUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class DemoRpcHandler extends AdvancedSubModule {

    @Override
    public void initDependent() throws Exception {
        super.initDependent();

        useRpcRecv().onRequestResponseTemplate(RpcTestPath.RR_BLOCK_KRYO, IntRef.class, (h, input) -> {
            log.info("收到游戏服发来的 {} 要求阻塞 {} 秒", h.path(), input);
            int seconds = input.v;
            if(seconds < 0) {
                seconds = 1;
            }
            if(seconds > 30) {
                seconds = 30;
            }
            while(seconds > 0) {
                log.info("倒计时 {} ", seconds--);
                ThreadUtils.sleep(1000);
            }
            return new IntRef(seconds);
        });

        useRpcRecv().onRequestResponseTemplate(RpcTestPath.RR_MANY_SMALL_DATA_KRYO, TheCat.class, (h, input) -> {
            //log.info("收到游戏服发来的 {} {}", h.path(), input);
            return new IntRef(input.age);
        });

        useRpcRecv().onRequestResponseTemplate(RpcTestPath.RR_CAT_KRYO_2_DOG_KRYO, TheCat.class, (h, input) -> {
            log.info("收到游戏服发来的 {} {}", h.path(), input);
            TheDog dog = new TheDog();
            dog.name = "阿汪.kryo";
            dog.master = "小1";
            return dog;
        });

        useRpcRecv().onRequestResponseTemplate(RpcTestPath.RR_CAT_JSON_2_DOG_JSON, TheCat.class, (h, input) -> {
            log.info("收到游戏服发来的 {} {}", h.path(), input);
            TheDog dog = new TheDog();
            dog.name = "阿汪.json";
            dog.master = "小2";
            return dog;
        });

        useRpcRecv().onRequestResponseTemplate(RpcTestPath.RR_CAT_KRYO_2_DOG_JSON, TheCat.class, (h, input) -> {
            log.info("收到游戏服发来的 {} {}", h.path(), input);
            TheDog dog = new TheDog();
            dog.name = "阿汪.json";
            dog.master = "小3";
            return dog;
        });

        useRpcRecv().onRequestResponseTemplate(RpcTestPath.RR_CAT_JSON_2_DOG_KRYO, TheCat.class, (h, input) -> {
            log.info("收到游戏服发来的 {} {}", h.path(), input);
            TheDog dog = new TheDog();
            dog.name = "阿汪.kryo";
            dog.master = "小4";
            return dog;
        });

        useRpcRecv().onRequestResponseTemplate(RpcTestPath.RR_TEXT_2_DOG_JSON, String.class, (h, input) -> {
            log.info("收到游戏服发来的 {} {}", h.path(), input);
            TheDog dog = new TheDog();
            dog.name = "阿汪.json";
            dog.master = "小5";
            return dog;
        });

        useRpcRecv().onRequestResponseTemplate(RpcTestPath.RR_CAT_JSON_2_TEXT, TheCat.class, (h, input) -> {
            log.info("收到游戏服发来的 {} {}", h.path(), input);
            return "阿汪.text";
        });

        useRpcRecv().onFireAndForgetTemplate(RpcTestPath.FAF_PLAY_SIDE_KRYO, PlaySide.class, (h, input) -> {
            log.info("收到游戏服发来的 {} ps = {}", h.path(), input);
        });
    }

    public Object play() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("world", playWithWorld());
        return map;
    }

    /** 与世界服进行交互 */
    private Object playWithWorld() throws Exception {
        log.info("与世界服进行交互");
        List<String> events = new ArrayList<>();
        DemoAppleVo apple = DemoAppleVo.random();
        events.add("生产出一个苹果:" + apple);
        byte[] helloResp = useRpcSend().world().requestResponse(RpcMessage.build(RpcDebugPath.GAME_RR_WORLD, apple));
        DemoBananaVo banana = RpcMessage.read(RpcDebugPath.GAME_RR_WORLD, helloResp).getResponse(DemoBananaVo.class);
        events.add("收到world返回的香蕉:" + banana);
        return events;
    }

    @Override
    public void onShutdown() throws Exception {
        log.info("演示子模块:rpc:onShutdown()");
    }
}
